package com.huopaolan.yohuoshop.Mvp.View.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;;

import com.bumptech.glide.Glide;
import com.huopaolan.lib_core.Base.BaseActivity;
import com.huopaolan.yohuoshop.Adapter.CommodityDetaAdapter;
import com.huopaolan.yohuoshop.Adapter.ViewPagerAdapter.CommodityDetaViewPagerAdapter;
import com.huopaolan.yohuoshop.Bean.BaseEntity;
import com.huopaolan.yohuoshop.Bean.CommodityDetailsEntity;
import com.huopaolan.yohuoshop.Di.Compont.DaggerCommodityDetaCompont;
import com.huopaolan.yohuoshop.Di.Modules.CommodityDetaModules;
import com.huopaolan.yohuoshop.Mvp.Contract.CommodityDetaContract;
import com.huopaolan.yohuoshop.Mvp.Presenter.CommodityDetaPresenter;
import com.huopaolan.yohuoshop.NetWork.API;
import com.huopaolan.yohuoshop.R;
import com.huopaolan.yohuoshop.Sp.SpUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.ArrayList;
import java.util.List;

public class CommodityDetailsActivity extends BaseActivity<CommodityDetaPresenter> implements CommodityDetaContract.CommodityDetaIView, OnClickListener {
    private View headView;
    private View centerView;
    private Button cart_button;
    private ImageView back_home;
    private ViewPager good_pager;
    private ImageView closePop;
    private TextView shop_count_plus, shop_count_subtract, shop_num;
    private RecyclerView good_deta_recycler;
    private LinearLayout commodity_head, commodity_service, commodity_store, commodity_collect;
    private PopupWindow popupWindow, window;
    private ImageView commodity_image, commodity_share, commodity_menu;
    private int count = 1;
    private String image_shop;
    private CommodityDetaAdapter commodityDetaAdapter;
    private CommodityDetaViewPagerAdapter commodityDetaViewPagerAdapter;
    private boolean flag = true;
    /**
     * popupWindow控件
     */
    private ImageView getShopImage;
    private TextView getShop_money, getShop_color;
    private Button getCart_button;
    private List<RadioButton> getRadio = new ArrayList<>();
    private String getSize, getShop_name, getShop_id;
    //    private int bannerHeight = 0;
//    private int categoryHeight = 0;
//    private int totalY = 0;
    private int height = 100;// 滑动开始变色的高,真实项目中此高度是由广告轮播或其他首页view高度决定
    private int overallXScroll = 0;

    @Override
    public int layoutID() {
        return R.layout.acitivity_commodity_deta;
    }

    @Override
    public void initView() {
        //初始化
        good_deta_recycler = findViewById(R.id.good_deta_recycler);
        commodity_service = findViewById(R.id.commodity_now_service);
        commodity_store = findViewById(R.id.commodity_now_store);
        commodity_collect = findViewById(R.id.commodity_now_collect);
        commodity_share = findViewById(R.id.commodity_share);
        commodity_menu = findViewById(R.id.activity_goods_details_iv_share);
        commodity_menu.setOnClickListener(this);
        commodity_share.setOnClickListener(this);
        commodity_service.setOnClickListener(this);
        commodity_store.setOnClickListener(this);
        commodity_collect.setOnClickListener(this);
        commodity_head = findViewById(R.id.re1);
        commodity_image = findViewById(R.id.commodity_image);
        commodity_image.setOnClickListener(this);
        cart_button = findViewById(R.id.commodity_shop_cart);
        cart_button.setOnClickListener(this);
        back_home = findViewById(R.id.commodity_back);
        back_home.setOnClickListener(this);
        //添加头布局
        headView = LayoutInflater.from(this).inflate(R.layout.item_head_goods_values, null);
        good_pager = headView.findViewById(R.id.vp_item_head_goods_values);
        //添加中布局
        centerView = LayoutInflater.from(this).inflate(R.layout.item_good_values_message, null);
        //配置适配器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        good_deta_recycler.setLayoutManager(layoutManager);
        good_deta_recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                overallXScroll = overallXScroll + dy;// 累加y值 解决滑动一半y值为0
                if (overallXScroll <= 0) {   //设置标题的背景颜色
                    commodity_head.setBackgroundColor(0);
                } else if (overallXScroll > 0 && overallXScroll <= height) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
                    float scale = (float) overallXScroll / height;
                    float alpha = (255 * scale);
                    Toast.makeText(CommodityDetailsActivity.this, "" + alpha, Toast.LENGTH_SHORT).show();
                    commodity_head.setBackgroundColor(Color.argb((int) alpha, 58, 58, 58));
                } else {
                    commodity_head.setBackgroundColor(Color.argb((int) 200, 58, 58, 58));
                }
            }
        });
        commodityDetaAdapter = new CommodityDetaAdapter(R.layout.item_my_shop);
        commodityDetaAdapter.setHeaderView(headView, 0);
        commodityDetaAdapter.setHeaderView(centerView, 1);
        good_deta_recycler.setAdapter(commodityDetaAdapter);
    }

    @Override
    protected void onStart() {
        Window window = getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void initData() {
        p.GoodReuqest();
    }

    @Override
    public void setUpDagger() {
        DaggerCommodityDetaCompont.builder()
                .commodityDetaModules(new CommodityDetaModules(this))
                .build().inject(this);
    }

    @Override
    public void CommodityDetaSuccess(CommodityDetailsEntity entity) {
        getShop_id = entity.recommend_goods.get(0).goods_id;
        getShop_name = entity.recommend_goods.get(0).goods_name;
        image_shop = API.BaseUrl + entity.recommend_goods.get(0).goods_img_path;
        List<ImageView> stringList = new ArrayList<>();
        for (int i = 0; i < entity.recommend_goods.size(); i++) {
            ImageView imageView = new ImageView(this);
            Glide.with(this).load(API.BaseUrl + entity.recommend_goods.get(i).goods_img_path).into(imageView);
            stringList.add(imageView);
        }
        commodityDetaViewPagerAdapter = new CommodityDetaViewPagerAdapter(stringList);
        good_pager.setAdapter(commodityDetaViewPagerAdapter);
    }

    @Override
    public void CommodityAddCarSuccess(BaseEntity entity) {
        if (entity == null) {
            return;
        }
        Toast.makeText(this, "" + entity.msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    protected void onDestroy() {
        finish();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.commodity_shop_cart:
                PopupWindowShop();
                break;
            case R.id.iv_item_bottomaddcar_close:
                popupWindow.dismiss();
                break;
            case R.id.commodity_back:
                finish();
                break;
            case R.id.tv_item_bottom_addcar_num_subtract:
                count--;
                if (count <= 0) {
                    Toast.makeText(this, "数量不能小于1", Toast.LENGTH_SHORT).show();
                    return;
                }
                shop_num.setText("" + count);
                break;
            case R.id.tv_item_bottom_addcar_num_add:
                count++;
                if (count <= 0) {
                    count = 1;
                    break;
                }
                shop_num.setText("" + count);
                break;
            case R.id.commodity_image:
                startActivity(new Intent(this, ShoppingTrolleyActivity.class));
                break;
            case R.id.commodity_now_service:
                startActivity(new Intent(this, ServiceChatActivity.class));
                break;
            case R.id.commodity_now_collect:
                break;
            case R.id.commodity_now_store:
                break;
            case R.id.commodity_share:
                new ShareAction(this).withText(image_shop).setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN).setCallback(shareListener).open();
                break;
            case R.id.activity_goods_details_iv_share:
                if (flag == true) {
                    showPopupMenu();
                    flag = false;
                } else {
                    window.dismiss();
                    flag = true;
                }
                break;
            case R.id.btn_item_bottom_addcar:
                String spReadUser = SpUtils.getInstance().SpReadUser();
                getShop_ALL_Date(spReadUser, getShop_id, getShop_name, getShop_money.getText().toString().trim(), getShop_color.getText().toString(), getSize, count);
                popupWindow.dismiss();
                Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
                break;
            case R.id.cb_item_bottom_addxar_size_L:
                getSize = getRadio.get(0).getText().toString();
                break;
            case R.id.cb_item_bottom_addxar_size_XL:
                getSize = getRadio.get(1).getText().toString();
                break;
            case R.id.cb_item_bottom_addxar_size_XXL:
                getSize = getRadio.get(2).getText().toString();
                break;
            case R.id.cb_item_bottom_addxar_size_XXXL:
                getSize = getRadio.get(3).getText().toString();
                break;
        }
    }

    private void showPopupMenu() {
        window = new PopupWindow(this, null, R.style.Transparent_Dialog);
        View inflate = LayoutInflater.from(this).inflate(R.layout.item_goods_values_more_menu, null);
        window.setContentView(inflate);
        window.setWidth(350);
        window.setHeight(580);
        window.setOutsideTouchable(false);
        window.setFocusable(false);
        window.showAtLocation(inflate, Gravity.RIGHT | Gravity.TOP, 0, 120);
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(CommodityDetailsActivity.this, "成功", Toast.LENGTH_SHORT).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(CommodityDetailsActivity.this, "失败", Toast.LENGTH_SHORT).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(CommodityDetailsActivity.this, "取消", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    private void PopupWindowShop() {
        //popuwindow选择
        popupWindow = new PopupWindow(this, null, R.style.Transparent_Dialog);
        //设置属性
        View inflate = LayoutInflater.from(this).inflate(R.layout.item_bottom_addcar, null);
        PopupInitView(inflate);
        //设置布局
        popupWindow.setContentView(inflate);
        //设置宽高
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //点击外部消失
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(false);
        //点击显示
        popupWindow.showAtLocation(inflate, Gravity.BOTTOM, 0, 0);
    }

    private void PopupInitView(View inflate) {
        //初始化控件
        getShopImage = inflate.findViewById(R.id.iv_item_bottom_addcar_img);
        getShop_money = inflate.findViewById(R.id.tv_item_bottom_addcar_goods_discount_price);
        getShop_color = inflate.findViewById(R.id.tv_item_bottom_addcar_goods_color_select);
        getCart_button = inflate.findViewById(R.id.btn_item_bottom_addcar);
        getCart_button.setOnClickListener(this);
        getRadio.add((RadioButton) inflate.findViewById(R.id.cb_item_bottom_addxar_size_L));
        getRadio.add((RadioButton) inflate.findViewById(R.id.cb_item_bottom_addxar_size_XL));
        getRadio.add((RadioButton) inflate.findViewById(R.id.cb_item_bottom_addxar_size_XXL));
        getRadio.add((RadioButton) inflate.findViewById(R.id.cb_item_bottom_addxar_size_XXXL));
        closePop = inflate.findViewById(R.id.iv_item_bottomaddcar_close);
        shop_count_subtract = inflate.findViewById(R.id.tv_item_bottom_addcar_num_subtract);
        shop_count_plus = inflate.findViewById(R.id.tv_item_bottom_addcar_num_add);
        shop_num = inflate.findViewById(R.id.tv_item_bottom_addcar_num_number);
        shop_count_subtract.setOnClickListener(this);
        shop_count_plus.setOnClickListener(this);
        closePop.setOnClickListener(this);
        Glide.with(this).load(API.BaseUrl + image_shop).into(getShopImage);
        for (int i = 0; i < getRadio.size(); i++) {
            getRadio.get(i).setOnClickListener(this);
        }
        Toast.makeText(this, "" + getSize, Toast.LENGTH_SHORT).show();
    }

    //封装数据
    private void getShop_ALL_Date(String getUserId, String getGoodsid, String getName, String getMoney, String getColor, String getSize, int getCount) {
        float parseFloat = Float.parseFloat(getMoney);
        float getTotal = (parseFloat * getCount);
        p.AddCarRequest(getUserId, getGoodsid, getName, getColor, getSize, "" + getCount, "" + getTotal);
    }
}
